package com.mjk.common.web.getway.restful.interceptor.type.imports.impl;


import com.mjk.common.base.response.BeeResponse;
import com.mjk.common.tools.interceptor.InterceptorChain;
import com.mjk.common.tools.logger.BizLogger;
import com.mjk.common.util.RandomUtil;
import com.mjk.common.util.SnowFlake;
import com.mjk.common.util.StrUtils;
import com.mjk.common.web.getway.restful.interceptor.GetWayInterceptorReq;
import com.mjk.common.web.getway.restful.interceptor.annotation.BeeGetWayInterceptor;
import com.mjk.common.web.getway.restful.interceptor.type.GetWayInterceptor;
import com.mjk.common.web.getway.restful.interceptor.type.GetWayInterceptorType;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

@BeeGetWayInterceptor(value = 5, interceptorType = GetWayInterceptorType.IMPORT)
public class ImportFileTransferToInterceptor implements GetWayInterceptor {

    @Override
    public BeeResponse interceptor(GetWayInterceptorReq request,
                                   InterceptorChain<GetWayInterceptorReq, BeeResponse> next) {
        try {
            StandardMultipartHttpServletRequest fileRequest = (StandardMultipartHttpServletRequest) request.getRequest();
            if (CollectionUtils.isEmpty(fileRequest.getMultiFileMap())) {
                return next.proceed(request);
            }
            HashMap<String, ArrayList<String>> fileParams = new HashMap<>();
            for (Entry<String, List<MultipartFile>> entry : fileRequest.getMultiFileMap().entrySet()) {
                synchronized (this) {
                    MultipartFile file = fileRequest.getFile(entry.getKey());
                    if (null != file) {
                        String fileName = file.getOriginalFilename();
                        String fileEnd;
                        if (null != fileName && fileName.contains(".")) {
                            fileEnd = fileName.substring(fileName.lastIndexOf("."));
                        } else {
                            fileEnd = fileName;
                        }
                        String cName = SnowFlake.getUid();
                        String filePath = request.getUploadPath() + cName + fileEnd;
                        String url = request.getUploadPath() + cName + fileEnd;
                        //对于相同时间段里面 上传相同的文件名称 处理可能会出现问题，这里在文件名称后面加上一个时间戳用以区分
                        File f = new File(filePath);
                        file.transferTo(f);

                        String key1 = StrUtils.removeEndNum(entry.getKey());
                        ArrayList<String> urls = fileParams.get(key1);
                        if (CollectionUtils.isEmpty(urls)) {
                            urls = new ArrayList<>();
                            fileParams.put(key1, urls);
                        }
                        urls.add(url);
                    }
                }
            }
            List<String> allPath = new ArrayList<>();
            for (Entry<String, ArrayList<String>> entry : fileParams.entrySet()) {
                request.getParams().put(entry.getKey(), StrUtils.join(entry.getValue(), ","));
                allPath.addAll(entry.getValue());
            }
            request.setFileName(StrUtils.join(allPath, ","));
        } catch (Exception e) {
            BizLogger.error(e);
        }
        return next.proceed(request);
    }

}
